﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Micro.Hosting
{
    public class SimpleStartup: Startup
    {
        public override void Run(string[] args)
        {
            WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
            //跨域配置
            builder.Services.AddCors(options =>
            {
                options.AddPolicy("any",
                builder => builder.AllowAnyHeader()
                .AllowAnyMethod()
                .AllowAnyOrigin());
            });
            builder.WebHost.UseDefaultServiceProvider(opt => { opt.ValidateOnBuild = false; });
            builder.InitBuilder();
            //绑定URL

            this._HostEnvironment = builder.Environment;
            this._Configuration = builder.Configuration;
            this._ServiceCollection = builder.Services;

            this.ConfigureServices(_ServiceCollection);

            builder.UseHostBuild();

            var app = builder.Build();
            app.UseCors("any");//跨域
            app.UseCustomUrls();
            this.Configure(app);

            app.Run();
        }

        public override void ConfigureServices(IServiceCollection services)
        {
            // ASP.NET HttpContext dependency
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            services.AddControllers().AddMvcMicro();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            services.AddEndpointsApiExplorer();
        }

        public override void Configure(WebApplication app)
        {
            if (app.Environment.IsDevelopment())
            {
                // 在开发环境中，使用异常页面，这样可以暴露错误堆栈信息，所以不要放在生产环境。
                app.UseDeveloperExceptionPage();
            }
            //base.Configure(app);
        }
    }
}
